package com.sunyard.pcc.web.controller;

import com.sunyard.michiru.common.entity.BaseResult;
import com.sunyard.michiru.common.entity.ErrorCodeEnum;
import com.sunyard.pcc.common.bo.SysLogBO;
import com.sunyard.pcc.common.bo.SysLogQueryBO;
import com.sunyard.pcc.common.validator.ValidationUtils;
import com.sunyard.pcc.core.service.LogService;
import com.sunyard.pcc.dal.query.SysLogQuery;
import com.sunyard.pcc.web.form.LogForm;
import com.sunyard.pcc.web.vo.LogVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Hewei
 * @Description:
 * @Date: Created in 16:49 2018/4/12
 */
@Slf4j
@RestController
@RequestMapping(value = "log")
public class LogController {

    @Autowired
    private LogService logService;

    /**
     *
     * @param logForm
     * @return
     */
    @ApiOperation(value = "条件查询历史记录列表")
    @ApiImplicitParam(value = "查询条件", paramType = "body", name = "logForm", dataType = "LogForm")
    @PostMapping(value = "/logList")
    public BaseResult<List<LogVO>> getLogsByConditional(@RequestBody LogForm logForm){
        //非空校验
        ValidationUtils.validateEntity(logForm);

        SysLogQueryBO sysLogQueryBO = new SysLogQueryBO();
        BeanUtils.copyProperties(logForm, sysLogQueryBO);
        List<SysLogBO> logBOS = logService.getLogListByCondition(sysLogQueryBO);
        List<LogVO> logs = new ArrayList<>();
        for (SysLogBO logBO : logBOS) {
            LogVO logVO = new LogVO();
            BeanUtils.copyProperties(logBO, logVO);
            logs.add(logVO);
        }

        //查询总条数
        Integer count = logService.getLogCountByCondition(sysLogQueryBO);

        BaseResult<List<LogVO>> result = BaseResult.create(logs, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());

        result.setTotalCount(count);
        result.setPageNo(logForm.getPageNo());
        result.setPageSize(logForm.getPageSize());
        return result;
    }

}
